package com.e2oto.seller.modules.launch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.e2oto.seller.common.base.BaseActivity;
import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.utils.LogTools;
import com.e2oto.seller.common.utils.RegexTools;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.launch.bean.CodeBean;
import com.e2oto.seller.modules.launch.presenter.ForgetPresenter;
import com.e2oto.seller.modules.launch.views.ForgetView;
import com.e2oto.seller.widget.LoginEditText;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class ForgetActivity extends BaseMvpActivity<ForgetView, ForgetPresenter> implements ForgetView {

    @BindView(R.id.forget_phone_et)
    LoginEditText phoneEt;
    @BindView(R.id.forget_code_et)
    EditText codeEt;
    @BindView(R.id.forget_get_code)
    TextView getCodeTv;


    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
    }

    @Override
    protected ForgetPresenter createPresenter() {
        return new ForgetPresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_forget;
    }

    @OnClick({R.id.toolbar_left_btn, R.id.forget_get_code, R.id.forget_commit_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_left_btn:
                finish();
                break;
            case R.id.forget_get_code:
                String phone = phoneEt.getEditText().getText().toString().trim();
                if (!RegexTools.isMobileExact(phone)) {
                    showMessage("请输入正确的手机号码");
                    return;
                }
                mPresenter.sendCode(phone);
                break;
            case R.id.forget_commit_btn:
                String mobile = phoneEt.getEditText().getText().toString().trim();
                String code = codeEt.getText().toString().trim();
                if (!RegexTools.isMobileExact(mobile)) {
                    showMessage("请输入正确的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    showMessage("请输入验证码");
                    return;
                }
                if (code.length() < 4) {
                    showMessage("验证码长度不能小于4");
                    return;
                }

                Intent intent = new Intent(this, SetPasswordActivity.class);
                intent.putExtra("code", code);
                intent.putExtra("mobile", mobile);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    @Override
    public void sendCodeResult(CodeBean codeBean) {
        LogTools.e("验证码:" + codeBean.getCode());
        //倒计时
        final int count = 60;
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(@NonNull Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        getCodeTv.setTextColor(ContextCompat.getColor(ForgetActivity.this, R.color.theme_color));
                        getCodeTv.setClickable(false);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        getCodeTv.setText("验证码已发送" + aLong);
                    }

                    @Override
                    public void onError(Throwable e) {
                        showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        getCodeTv.setTextColor(ContextCompat.getColor(ForgetActivity.this, R.color.black));
                        getCodeTv.setClickable(true);
                        getCodeTv.setText("获取验证码");
                    }
                });
    }


}
